package unimelb.filesync.common.message;

import org.json.simple.JSONObject;

import filesync.Instruction;

public class FileSyncRequest implements Jsonable, Comparable<FileSyncRequest> {

    private final Instruction instruction;

    private final long sequenceId;

    private final long instructionId;

    public FileSyncRequest(Instruction instruction, long sequenceId, long instructionId) {
        super();
        this.instruction = instruction;
        this.sequenceId = sequenceId;
        this.instructionId = instructionId;
    }

    public Instruction getInstruction() {
        return instruction;
    }

    public long getSequenceId() {
        return sequenceId;
    }

    public long getInstructionId() {
        return instructionId;
    }

    @SuppressWarnings("unchecked")
    @Override
    public String toJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sequenceId", sequenceId);
        jsonObject.put("instructionId", instructionId);
        jsonObject.put("instruction", instruction.ToJSON());
        return jsonObject.toJSONString();
    }

    @Override
    public int compareTo(FileSyncRequest o) {
        if (this.sequenceId < o.sequenceId) {
            return -1;
        } else if (this.sequenceId > o.sequenceId) {
            return 1;
        } else if (this.instructionId < o.instructionId) {
            return -1;
        } else if (this.instructionId > o.instructionId) {
            return 1;
        } else {
            return 0;
        }
    }

}
